// DbDelete.cpp
#include "DbDelete.h"
#include <iostream>
#include <sstream>

// --- 删除方法实现 ---

bool DbDelete::DeleteUser(int32_t user_id, std::string& error_message) {
    try {
        // 使用 DETACH DELETE 删除节点及其所有关系
        const std::string cypher_query =
            "MATCH (u:User {id: $target_id}) "
            "DETACH DELETE u"; // DETACH 确保删除节点及其所有连接的关系

        // 创建参数
        neo4j_map_entry_t entries[] = {
            { .key = neo4j_string("target_id"), .value = neo4j_int(user_id) }
        };
        neo4j_value_t params_value = neo4j_map(entries, 1);

        // 执行查询
        DbClient& db_client = DbClient::getInstance();
        neo4j_result_stream_t* result = db_client.run(cypher_query.c_str(), params_value);
        
        if (result == NULL) {
            error_message = "Failed to execute DeleteUser query";
            std::cerr << error_message << std::endl;
            return false;
        }

        // 释放结果流
        neo4j_close_results(result);

        std::cout << "Successfully deleted User node with ID: " << user_id << std::endl;
        return true;

    } catch (const std::exception& e) {
        error_message = "DeleteUser failed: ";
        error_message += e.what();
        std::cerr << error_message << std::endl;
        return false;
    } catch (...) {
        error_message = "DeleteUser failed: Unknown error occurred.";
        std::cerr << error_message << std::endl;
        return false;
    }
}

bool DbDelete::DeleteList(int32_t list_id, std::string& error_message) {
    try {
        // 同样使用 DETACH DELETE
        const std::string cypher_query =
            "MATCH (l:List {id: $target_id}) "
            "DETACH DELETE l";

        // 创建参数
        neo4j_map_entry_t entries[] = {
            { .key = neo4j_string("target_id"), .value = neo4j_int(list_id) }
        };
        neo4j_value_t params_value = neo4j_map(entries, 1);

        // 执行查询
        DbClient& db_client = DbClient::getInstance();
        neo4j_result_stream_t* result = db_client.run(cypher_query.c_str(), params_value);
        
        if (result == NULL) {
            error_message = "Failed to execute DeleteList query";
            std::cerr << error_message << std::endl;
            return false;
        }

        // 释放结果流
        neo4j_close_results(result);

        std::cout << "Successfully deleted List node with ID: " << list_id << std::endl;
        return true;

    } catch (const std::exception& e) {
        error_message = "DeleteList failed: ";
        error_message += e.what();
        std::cerr << error_message << std::endl;
        return false;
    } catch (...) {
        error_message = "DeleteList failed: Unknown error occurred.";
        std::cerr << error_message << std::endl;
        return false;
    }
}

bool DbDelete::DeleteTodo(int32_t todo_id, std::string& error_message) {
    try {
        const std::string cypher_query =
            "MATCH (t:Todo {id: $target_id}) "
            "DELETE t"; // Todo 节点通常不直接连接其他节点，普通 DELETE 即可

        // 创建参数
        neo4j_map_entry_t entries[] = {
            { .key = neo4j_string("target_id"), .value = neo4j_int(todo_id) }
        };
        neo4j_value_t params_value = neo4j_map(entries, 1);

        // 执行查询
        DbClient& db_client = DbClient::getInstance();
        neo4j_result_stream_t* result = db_client.run(cypher_query.c_str(), params_value);
        
        if (result == NULL) {
            error_message = "Failed to execute DeleteTodo query";
            std::cerr << error_message << std::endl;
            return false;
        }

        // 释放结果流
        neo4j_close_results(result);

        std::cout << "Successfully deleted Todo node with ID: " << todo_id << std::endl;
        return true;

    } catch (const std::exception& e) {
        error_message = "DeleteTodo failed: ";
        error_message += e.what();
        std::cerr << error_message << std::endl;
        return false;
    } catch (...) {
        error_message = "DeleteTodo failed: Unknown error occurred.";
        std::cerr << error_message << std::endl;
        return false;
    }
}

bool DbDelete::DeleteComment(int32_t comment_id, std::string& error_message) {
    try {
        const std::string cypher_query =
            "MATCH (c:Comment {id: $target_id}) "
            "DELETE c"; // Comment 节点通常不直接连接其他节点，普通 DELETE 即可

        // 创建参数
        neo4j_map_entry_t entries[] = {
            { .key = neo4j_string("target_id"), .value = neo4j_int(comment_id) }
        };
        neo4j_value_t params_value = neo4j_map(entries, 1);

        // 执行查询
        DbClient& db_client = DbClient::getInstance();
        neo4j_result_stream_t* result = db_client.run(cypher_query.c_str(), params_value);
        
        if (result == NULL) {
            error_message = "Failed to execute DeleteComment query";
            std::cerr << error_message << std::endl;
            return false;
        }

        // 释放结果流
        neo4j_close_results(result);

        std::cout << "Successfully deleted Comment node with ID: " << comment_id << std::endl;
        return true;

    } catch (const std::exception& e) {
        error_message = "DeleteComment failed: ";
        error_message += e.what();
        std::cerr << error_message << std::endl;
        return false;
    } catch (...) {
        error_message = "DeleteComment failed: Unknown error occurred.";
        std::cerr << error_message << std::endl;
        return false;
    }
}

bool DbDelete::DeleteEntity(const std::string& entity_id, std::string& error_message) {
    try {
        const std::string cypher_query =
            "MATCH (e:Entity {id: $target_id}) "
            "DETACH DELETE e"; // Entity 可能与其他节点有关系，使用 DETACH DELETE

        // 创建参数
        neo4j_map_entry_t entries[] = {
            { .key = neo4j_string("target_id"), .value = neo4j_string(entity_id.c_str()) }
        };
        neo4j_value_t params_value = neo4j_map(entries, 1);

        // 执行查询
        DbClient& db_client = DbClient::getInstance();
        neo4j_result_stream_t* result = db_client.run(cypher_query.c_str(), params_value);
        
        if (result == NULL) {
            error_message = "Failed to execute DeleteEntity query";
            std::cerr << error_message << std::endl;
            return false;
        }

        // 释放结果流
        neo4j_close_results(result);

        std::cout << "Successfully deleted Entity node with ID: " << entity_id << std::endl;
        return true;

    } catch (const std::exception& e) {
        error_message = "DeleteEntity failed: ";
        error_message += e.what();
        std::cerr << error_message << std::endl;
        return false;
    } catch (...) {
        error_message = "DeleteEntity failed: Unknown error occurred.";
        std::cerr << error_message << std::endl;
        return false;
    }
}